<template>
  <tm-app ref="app">
    <breadCrumb :dataList="data.titleArray"></breadCrumb>
    <personList :setList="data.listInfo" @btnClick="goMenu"></personList>
  </tm-app>
</template>

<script lang="ts" setup>
import { onLoad, onShow } from '@dcloudio/uni-app'
import { reactive } from 'vue'
import * as config from '../config/menuOperate'
import personList from '../components/personList.vue'
import breadCrumb from '../../../components/breadCrumb/index.vue'
const data = reactive<any>({
  listInfo: [],
  titleArray: [],
  queryItem: '',
  nextLength: 0
})
onShow(() => {
  /* eslint-disable */
  const pages = getCurrentPages()
  let titleArray: any = [] //标题数据
  pages.forEach((_item, index) => {
    if (index > 0) {
      const currentPage: any = pages[pages.length - index]
      const options = currentPage.options
      let data: any = JSON.parse(options.item)
      let jsons: any = {}
      jsons = {
        name: data.label
      }
      titleArray.push(jsons)
    }
  })
  data.titleArray = titleArray.reverse()
})

onLoad(async query => {
  if (query && query.item) {
    uni.showLoading({ title: '加载中...' })
    let item = JSON.parse(query.item)
    let res = await config.loadSettingMenu()
    data.queryItem = item
    findObject(res.children, item.key, '')
  }
})
async function findObject(arr: any[], key: any, type: any) {
  arr.forEach(item => {
    if (item.key === key) {
      // if (type === 'count') {
      //   getResult(item)
      // } else {
      getMenuList(item)
      // }
    }
    if (item.children && item.children.length > 0) {
      findObject(item.children, key, type)
    }
  })
}
// function getResult(obj: any) {
//   data.nextLength = obj.children.length
// }
async function getMenuList(obj: {
  item: { directory: { memberDirectory: any }; target: { members: any[] } }
  children: any[]
}) {
  let arr: {
    label: any
    itemType: any
    icon: any
    remark: any
    isShow: boolean
    key: any
    code: any
    createTime: any
    updateTime: any
  }[] = []
  obj.children?.forEach(element => {
    let obj = {
      label: element.label,
      itemType: element.item.target.metadata.typeName,
      icon: element?.item?.share?.avatar?.thumbnail,
      remark: element.remark,
      isShow: true,
      key: element.key,
      code: element.item.target.code,
      createTime: element.item.target.metadata.createTime,
      updateTime: element.item.target.updateTime
    }
    arr.push(obj)
  })
  uni.hideLoading()
  if (data.queryItem.itemType === 'next') {
    data.listInfo = arr
  } else {
    data.listInfo = arr.filter(item => {
      return item.itemType.includes(data.queryItem.label)
    })
  }
}

async function goMenu(
  item: {
    key: any
    label: any
    type: any
    mainKey: any
    itemType: any
    code: any
    createTime: any
    updateTime: any
    remark: any
  },
  type: any
) {
  // let res = await config.loadSettingMenu()
  // data.queryItem = item
  // findObject(res.children, item.key, 'count')
  const params = {
    key: item.key,
    label: item.label,
    itemType: data.nextLength > 0 ? 'next' : 'member'
  }

  if (type === 'menuDetail') {
    const params = {
      name: item.label,
      typeName: item.itemType,
      code: item.code,
      createTime: item.createTime,
      updateTime: item.updateTime,
      remark: item.remark
    }
    uni.navigateTo({
      url: '/pages/relation/menuDetail?item=' + JSON.stringify(params)
    })
  } else {
    // if (data.nextLength > 0) {
    //   //还有下级
    //   uni.navigateTo({
    //     url: '/pages/relation/group/groupIndex?item=' + JSON.stringify(params)
    //   })
    // } else {
      //无下级
    uni.navigateTo({
      url: '/pages/relation/group/groupList?item=' + JSON.stringify(params)
    })
    // }
  }
}
</script>
<style lang="scss">
@import '../../../common/style/global.scss';
</style>
